#include "../../include/net_interface.hpp"
#ifdef WIN32
#include "./iocp/iocpnetmgr.h"
#else
#include "./epoll/epollnetmgr.h"
#endif

LIBVERSION __net_libversion = net_libversion;

static bool CheckVersion(LIBVERSION libversion)
{
    if (__net_libversion == libversion)
    {
        return true;
    }

    return false;
}

INetManager* CreateNetManager(ENetIOType eNetIOType, ISessionManager* poSessionManager, LIBVERSION libversion)
{
    if (!CheckVersion(libversion))
    {
        return NULL;
    }

    switch(eNetIOType)
    {
    case NET_IO_COMPLETEPORT:
        {
#ifdef WIN32
            CIocpNetMgr* poIOCPNetManager = new CIocpNetMgr;
            if (NULL != poIOCPNetManager)
            {
                if (!poIOCPNetManager->Init(poSessionManager))
                {
                    delete poIOCPNetManager;
                    poIOCPNetManager = NULL;
                }
            }

            return poIOCPNetManager;
#endif
        }
        break;
    case NET_IO_EPOLL:
        {
#ifndef WIN32
            CEpollNetMgr* poEpollNetManager = new CEpollNetMgr;
            if (NULL != poEpollNetManager)
            {
                if (!poEpollNetManager->Init(poSessionManager))
                {
                    delete poEpollNetManager;
                    poEpollNetManager = NULL;
                }
            }

            return poEpollNetManager;
#endif
        }
        break;
    case NET_IO_ASYNCSELECT:
        {
#ifdef WIN32
            //CAsyncNetManager* poAsyncNetManager = new CAsyncNetManager;
            //if (NULL != poAsyncNetManager)
            //{
            //    if (!poAsyncNetManager->Init(poSessionManager))
            //    {
            //        delete poSessionManager;
            //        poSessionManager = NULL;
            //    }
            //}

            //return poAsyncNetManager;
            return NULL;
#endif
        }
        break;
    case NET_IO_SELECT:
#ifdef WIN32
        {
            //CSelectNetManager* poNetManager = new CSelectNetManager;
            //if (NULL != poNetManager)
            //{
            //    if (!poNetManager->Init(poSessionManager))
            //    {
            //        delete poNetManager;
            //        poNetManager = NULL;
            //    }
            //}

            //return poNetManager;
            return NULL;
        }
#endif
        break;
    default:
        break;
    }

    return NULL;
}

#ifdef WIN32
void SetNetIOCPSetting( INetManager* poNetManager, EIOCPSetting eSetting, INT32 iValue )
{
    switch (poNetManager->GetIOType())
    {
    case NET_IO_COMPLETEPORT:
        {
            CIocpNetMgr* net_mgr = (CIocpNetMgr*)poNetManager;

            net_mgr->SetIocpSetting(eSetting, iValue);
        }
        break;
    }
}
#endif
